import React from 'react'
import { HashRouter, Route, Switch, Redirect } from 'react-router-dom'
import login from '../views/login/login'
import Detail from '../views/news/detail'
import News from '../views/news/News'
import NewsSandBox from '../views/sandbox/NewsSandBox'

export default function IndexRouter() {
    return (
        <HashRouter>
            {/* Switch就是匹配到第一个就不会去匹配了 */}
            <Switch>
                <Route path="/login" component={login}></Route>
                <Route path="/news" component={News} />
                <Route path="/detail/:id" component={Detail} />
                {/* 本地如果有token说明授过权，登陆过； */}
                {/*  Redirect（重定向）：三目运算符 如果没有token就重新到login页面  render去渲染NewsSandBox */}
                <Route path="/" render={() => [
                    localStorage.getItem("token") ? <NewsSandBox></NewsSandBox> : <Redirect to="/login" />
                ]}></Route>
            </Switch>
        </HashRouter>
    )
}
